package com.framework.boc.controller;

import com.framework.boc.error.CommonException;
import com.framework.boc.response.CommonResponse;
import com.framework.boc.response.ResponseEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
//import org.springframework.security.access.AccessDeniedException;

public class BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CommonResponse defaultExceptionHandler(HttpServletRequest request, Exception e) {
        logger.error(e.getMessage());
        e.printStackTrace();
        if (e instanceof CommonException) {
            CommonException commonException = (CommonException) e;
            return CommonResponse.error(commonException.getErrorCode(), commonException.getErrorMessage());
        } else {
            return CommonResponse.error(e.getMessage());
        }
    }


}
